//
//  UIImage+SY.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/9/17.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "UIImage+SY.h"

@implementation UIImage (SY)

+ (NSString *)saveImageCommentImageStr:(UIImage *)imageName {
    
    NSData *imageData = UIImagePNGRepresentation(imageName);
    //判断图像是png格式的还是jpg格式的（iOS相册里面可以保存gif格式的，可是gif格式的图片在相册中是以静态图显示的）
    if (UIImagePNGRepresentation(imageName) == nil)
    {
        //            imageData = UIImageJPEGRepresentation(tempImage, 0.5);
        imageData = UIImageJPEGRepresentation(imageName, 0.5);
    }
    else
    {
        imageData = UIImagePNGRepresentation(imageName);
    }
    
    NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *tempDocumentPath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"10245.png"]];
    //保存到 document
    [imageData writeToFile: tempDocumentPath atomically:NO];
    return tempDocumentPath;
}

+ (NSMutableArray *)saveImageCommentImageArr:(NSArray *)imageArr namePhoto:(NSString *)name {
    NSMutableArray *dataImageArr = [NSMutableArray array];
    for (NSInteger i = 0; i<imageArr.count; i++) {
        UIImage *tempImage = imageArr[i];
        
        NSData *imageData = UIImagePNGRepresentation(tempImage);
        
        //判断图像是png格式的还是jpg格式的（iOS相册里面可以保存gif格式的，可是gif格式的图片在相册中是以静态图显示的）
        if (UIImagePNGRepresentation(tempImage) == nil)
        {
//            imageData = UIImageJPEGRepresentation(tempImage, 0.5);
            imageData = UIImageJPEGRepresentation(tempImage, 1);
        }
        else
        {
            imageData = UIImagePNGRepresentation(tempImage);
        }
        
        NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *dataFilePath = [documentPath stringByAppendingPathComponent:@"ios"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isDir = NO;
        // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
        BOOL existed = [fileManager fileExistsAtPath:dataFilePath isDirectory:&isDir];
        
        if (!(isDir && existed) && ![fileManager fileExistsAtPath:dataFilePath]) {
            // 在Document目录下创建一个archiver目录
            [fileManager createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSString *tempDocumentPath = [dataFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_userHeader_%ld.png",name,i]];
        //保存到 document
        [imageData writeToFile: tempDocumentPath atomically:NO];
        
        [dataImageArr addObject:tempDocumentPath];
    }
    return dataImageArr;
}
@end
